<template>
  <!-- 添加或修改销售信息管理对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="客户名称" prop="customerId">
                <el-select v-model="form.customerId" placeholder="请选择客户" filterable @change="selectCustomer" clearable
                           style="width: 300px" disabled>
                  <el-option v-for="dict in dict.type.goods_customer" ref="myselectCustomer"
                             :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="供应商" prop="supplierId">
                <el-select v-model="form.supplierId" placeholder="请选择供应商" filterable @change="selectSupplier"
                           style="width: 300px" disabled>
                  <el-option v-for="dict in dict.type.goods_supplier" ref="myselectSsupplier"
                             :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="产品名称" prop="goodsName">
                <el-input v-model="form.goodsName" placeholder="请输入产品名称" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产品型号" prop="goodModel">
                <el-input v-model="form.goodModel" placeholder="请输入产品型号" disabled/>
              </el-form-item>
            </el-col>
            <!--<el-col :span="12">
              <el-form-item label="产品型号明细" prop="goodsBrand">
                <el-input v-model="form.goodsBrand" placeholder="请输入产品型号明细" disabled/>
              </el-form-item>
            </el-col>-->
            <el-col :span="12">
            <el-form-item label="销售单价" prop="goodsUnitPrice">
              <el-input-number v-model="form.goodsUnitPrice" :precision="2" placeholder="请输入销售单价" @change="updateTotlePrice" disabled/>
              <!--span style="color: red;margin-left: 20px;">采购单价:{{orderInfo.costPrice}}</span>-->
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="订单数量" prop="goodsCount">
            <el-input-number v-model="form.goodsCount" placeholder="请输入订单数量"  style="width: 200px;" @change="updateTotlePrice"/>
          </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="总价" prop="goodsTotalPrice">
                <el-input v-model="form.goodsTotalPrice" placeholder="请输入总价" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="成本价" prop="goodsTotalPrice">
                <el-input v-model="form.goodsTotalPrice" placeholder="请输入成本价" disabled/>
              </el-form-item>
            </el-col>


          </el-row>

          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {addAction, getById, updateAction} from '@/api/manage'
    import {listByLazyTree} from "@/api/sysArea/area";
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";

    export default {
        name: "GoodSaleForm",
        components: {
          Treeselect,
        },
        dicts: [ 'goods_supplier', 'goods_customer','fund_callback', 'yes_no'],
        data() {
            return {
                // 遮罩层
                loading: true,
                activeName: "first",
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                deptOptions: [],
                // 表单参数
                form: {},
                // 表单校验
                rules: {},
                url: {
                    getById: '/goodsSale/goodsSale/',
                    add: '/goodsSale/goodsSale/addOrder',
                    edit: '/goodsSale/goodsSale/editOrder',
                },
            };
        },
        created() {
          this.getTreeselect();
        },
        methods: {
          /** 查询部门下拉树结构 */
          getTreeselect() {
            //listAreaByLazyTree({parentId:3503}).then(response => {
            listByLazyTree().then(response => {
              this.deptOptions = response.data;
            });
          },
          updateTotlePrice() {
            let that = this;
            if (that.form.goodsCount && that.form.goodsUnitPrice) {
              let totlePrice = this.functionNumberMul(that.form.goodsCount, that.form.goodsUnitPrice);
              that.$set(that.form, "goodsTotalPrice", totlePrice);
            }
          },
          functionNumberMul(arg1, arg2) {
            var m = 0;
            var s1 = arg1.toString();
            var s2 = arg2.toString();
            try {
              m += s1.split(".")[1].length;
            } catch (e) {
            }
            try {
              m += s2.split(".")[1].length;
            } catch (e) {
            }
            return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
          },
          selectCustomer(value) {
            let that = this;
            that.$nextTick(() => {
              let arr = that.$refs.myselectCustomer;
              for (let i = 0; i < arr.length; i++) {
                if (arr[i].currentValue == value) {
                  that.form.customerName = arr[i].label;
                }
              }
            })
          },
          selectSupplier(value) {
            let that = this;
            that.$nextTick(() => {
              let arr = that.$refs.myselectSsupplier;
              for (let i = 0; i < arr.length; i++) {
                if (arr[i].currentValue == value) {
                  that.form.supplierName = arr[i].label;
                }
              }
            })
          },
          selectUser(value) {
            let that = this;
            that.$nextTick(() => {
              let arr = that.$refs.myselectUser;
              for (let i = 0; i < arr.length; i++) {
                if (arr[i].currentValue == value) {
                  that.form.saleName = arr[i].label;
                }
              }
            })
          },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    goodsId: null,
                    goodsName: null,
                    goodModel: null,
                    goodsBrand: null,
                    goodsUnitPrice: null,
                    goodsCount: null,
                    goodsTotalPrice: null,
                    saleDate: null,
                    supplierId: null,
                    supplierName: null,
                    customerId: null,
                    customerName: null,
                    invoice: null,
                    invoiceId: null,
                    invoiceNo: null,
                    fundCallback: null,
                    receipt: null,
                    saleId: null,
                    saleName: null,
                    delFlag: null,
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null,
                    remark: null
                };
                this.resetForm("form");
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加销售信息管理";
            },
          addGoodsSale(rows){
              this.reset();
              this.form.goodsId=rows.id;
              this.form.deptId=rows.deptId;
              this.form.deptName=rows.deptName;
              this.form.code=rows.code;
              this.form.goodsName=rows.name;
              this.form.goodModel=rows.model;
              this.form.goodsBrand=rows.brand;
              this.form.remark=rows.remark;
              this.open = true;
              this.title = "添加产品信息管理";
          },
            /** 修改按钮操作 */
            handleUpdateShow(row) {
                this.reset();
                const id = row.id || this.ids;
                getById(this.url.getById, id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改销售信息管理";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateAction(this.url.edit, this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        } else {
                            addAction(this.url.add, this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        }
                    }
                });
            },
        }
    };
</script>
